home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Amiga Collections: MegaDisc
/
MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].zip
/
MegaDisc 43 (1995-03)(MegaDisc Digital Publishing)(AU)(Disk 2 of 2)[m bamcopy].adf
/
Programming
/
Pascal_Tutes
/
Tute14.pas
< prev
next >
Wrap
Pascal/Delphi Source File
|
1995-01-27
|
8KB
|
258 lines
{
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» «»
«» TUTORIAL FOURTEEN «»
«» «»
«»
by
«»
«» «»
«»
Anthony Peck
«»
«» «»
«» «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43
I'm operating within acceptable parameters...
As well as accepting variables, procedures may define their own
variables (and constants). These cannot be accessed by any other part
of the program. They apply only within that procedure.
For example, if we wanted a procedure to count numbers, we could define
a variable within that procedure called
COUNT
, which would only be used
in that procedure.
Let's bung the rest of the theoretical ramblings and get stuck into some
juicy Pascal code...
}
Program MDTute14 (Input,Output);
{ This program finds the factors of a user entered number
Author : A N Peck
Date : 31 October 1994
Procedures used:
Title - prints up a fancy title on the screen
Getnumber - prompts user for a number
Convertnumber - makes sure the user has entered a
positive whole number
Findfacts - finds the factors for the number }
Var
entry: real;
number: longint;
{ ---------------------------------------------------------- }
Procedure Title;
{ Prints up a nice title on the screen }
Const
tab = ' ';
{
^
|
|__
Here a constant has been defined within the procedure. This
constant cannot be accessed by the rest of the program. For a
pictorial representation of the scope of locally defined
variables and constants, see Diagram_Six.
I was going to make Diagram_Six a picture of an egret in
flight, but I was convinced by the bloke across the road
that it was beyond the scope of these tutorials...
}
begin
writeln;
writeln(tab,tab,tab,'FACTOR FINDER!!');
writeln(tab,tab,tab,'---------------');
writeln;
writeln(tab,'This program will find all the factors of a number');
writeln;
writeln(tab,'entered by the user. For example, the factors');
writeln;
writeln(tab,'of 12 are 1,2,3,4,6 and 12');
writeln;
end; { Title }
{ ---------------------------------------------------------- }
Procedure Getnumber (Var userchoice: real);
{ Prompts user for a number }
begin
writeln;
write(' Please enter a whole number: ');
readln(userchoice);
end; { Getnumber }
{ ---------------------------------------------------------- }
Procedure Convertnumber
(realnumber: real; Var integernumber: longint);
{ Converts a real number to a positive integer }
begin
integernumber := Abs(Round(realnumber));
end; { Convertnumber }
{ ---------------------------------------------------------- }
Procedure Findfacts (maximum: longint);
{ Finds all the factors for the number passed }
Const
tab = ' ';
Var
count: longint;
{
^
|
|__
A locally defined variable is defined here. The variable
"count" is just used within this procedure to keep track of the
factors, and as a herbicide.
}
begin
writeln;
writeln(tab,'The factors of ',maximum,' are:');
writeln;
write(tab);
For count := 1 to maximum do
begin
if (maximum mod count = 0) then
write(count,' ');
end;
{
^
|
|__
The "engine" of this program, the algorithm that
finds the factors. If the number divided by "count"
yields no remainder, then that number must be a
factor, and is written. If not, the "For" loop
increments one and the next "count" is checked.
I'm just so amazed at how wonderful this algorithm is
that I think I'll set up a small shrine down in the
garden to celebrate its existence...
}
writeln;
writeln;
end; { Findfacts }
{ ---------------------------------------------------------- }
{ ---------------------------------------------------------- }
begin { Main Program }
entry := 0.00;
number := 0;
Title;
Getnumber(entry);
Convertnumber(entry,number);
Findfacts(number);
end. { Main Program }
{ ---------------------------------------------------------- }
{
It's fair to say that there are other (more efficient) ways of writing
this program. However, the point of this is a demonstration of scope.
If you want to fiddle with the algorithm, please feel free to do so in
the privacy of your own home.
For small programs, such discussions of scope seem a little out of
place, however it won't be long before you are writing programs which
are several hundred lines in length. It will help you debug such
programs if by then you have a clear understanding of MODULARITY
(keeping the program chopped into definable sections) and SCOPE. It
will also help if you can stick two lit incense sticks in your nostrils
and whistle "Waltzing Matilda" at the same time!
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
«» «»
«»
grail link elk trophus riddle
«»
«» «»
«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»«»
43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43 !¡ 43
}